package cn.usain.controller;


import cn.usain.pojo.User;
import cn.usain.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    @GetMapping("findAll")
    public String findAll(String name,Model model, @RequestParam(required = true,defaultValue = "1")Integer pageNum){
        Page<User> page =   userService.findPage(name,pageNum);
        model.addAttribute("list",page);
        return "user";
    }

    @PostMapping("add")
    public String add(User user){
        userService.add(user);
        return "redirect:findAll";
    }


    @GetMapping("toadd")
    public String toadd(){
        return "add";
    }

    //回显
    @GetMapping("findById")
    public String findById(Model model,int id){
        User user = userService.findById(id);
        model.addAttribute("user",user);
        return "update";
    }

    @PostMapping("update")
    public String update(User user){
        userService.update(user);
        return "redirect:findAll";
    }

    @GetMapping("del")
    public String del(int id){
        userService.del(id);
        return "redirect:findAll";
    }
}
